package gov.pbc.nn.core.utils;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.util.List;

import javax.persistence.Column;

import org.apache.commons.lang3.reflect.FieldUtils;

public class EntityRowMapperUtils {

	public static <T> T get(Class<T> clazz, ResultSet rs, int rowIndex) {
		try {
			T t = clazz.newInstance();
			List<Field> fields = FieldUtils.getAllFieldsList(clazz);
			for (Field f : fields) {
				Column c = f.getAnnotation(Column.class);
				if (c != null) {
					f.setAccessible(true);
					f.set(t, rs.getObject(c.name()));
				}
			}
			return t;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
